package com.glm.wwii.battleships.defence;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.AudioOptions;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import com.glm.wwii.battleships.gamelogic.CoreGame;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.util.Log;
import android.view.Display;
import android.view.Menu;

import java.lang.reflect.Method;


public class WWII extends BaseGameActivity {	
	private int cameraWidth, cameraHeight;
	private Engine mEngine;
	private AudioOptions mAudioOption= null;
	private EngineOptions mEngineOption = null;
	
	private Scene mMainScene;
	private Scene mSplashScene;	
    
	private CoreGame<?> mCoreGame;
	
	@SuppressWarnings("unused")
	private SceneType mCurrentScene = SceneType.SPLASH;
	
	private enum SceneType
	{
		SPLASH,
		MAIN,
		OPTIONS,
		WALL_ALL_FAME,
		LEVEL_SELECTION
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_wwii, menu);
        return true;
    }
    @SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	@Override
	public EngineOptions onCreateEngineOptions() {
		final Display display = getWindowManager().getDefaultDisplay();		
		boolean bNewAPI=false;
		int width = 0,height = 0;
		Point size = new Point();
		try {
			Class<?> obj = Class.forName("android.view.Display");
			Method m[] = obj.getDeclaredMethods();
            for (int i = 0; i < m.length; i++){
            	if(m[i].toString().compareToIgnoreCase("getSize")==0){
            		bNewAPI=true;
            	}
            }
            
		} catch (ClassNotFoundException e) {
			Log.e(this.getClass().getCanonicalName(), "Error Class Not Found!");
			e.printStackTrace();
		}
		if(bNewAPI){
			display.getSize(size);
			width = size.x;
			height = size.y;
		}else{
			height=display.getHeight();
			width=display.getWidth();			
		}
		this.cameraWidth = width;		
	    this.cameraHeight = height;

		final Camera camera = new Camera(0, 0, cameraWidth, cameraHeight);	
		
		mEngineOption = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
			    new RatioResolutionPolicy(cameraWidth, cameraHeight), camera);
		mAudioOption= mEngineOption.getAudioOptions();
		mAudioOption.setNeedsMusic(true);
		mAudioOption.setNeedsSound(true);
		
		mCoreGame = new CoreGame<Object>(getApplicationContext(), this,this.cameraHeight,this.cameraWidth);
		
		return mEngineOption;
	}

	/* (non-Javadoc)
	 * @see org.andengine.ui.IGameInterface#onCreateResources(org.andengine.ui.IGameInterface.OnCreateResourcesCallback)
	 */
	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		 	
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}
	/* (non-Javadoc)
	 * @see org.andengine.ui.IGameInterface#onCreateScene(org.andengine.ui.IGameInterface.OnCreateSceneCallback)
	 */
	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		// 1 - Create new scene
		mSplashScene = new Scene();	
		mCoreGame.initSplashScene(mSplashScene);
        pOnCreateSceneCallback.onCreateSceneFinished(this.mSplashScene);
	}
	/* (non-Javadoc)
	 * @see org.andengine.ui.IGameInterface#onPopulateScene(org.andengine.entity.scene.Scene, org.andengine.ui.IGameInterface.OnPopulateSceneCallback)
	 */
	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		
		mEngine.registerUpdateHandler(new TimerHandler(3f, new ITimerCallback() 
		{
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                mMainScene = new Scene();	
                mCoreGame.LoadGameResource();
                mCoreGame.initMain(mMainScene,mEngine);         
                
                mEngine.setScene(mMainScene);
                mCurrentScene = SceneType.MAIN;
            }
		}));
  
		pOnPopulateSceneCallback.onPopulateSceneFinished();
		
		
	}
	/* (non-Javadoc)
	 * @see org.andengine.ui.activity.BaseGameActivity#onCreateEngine(org.andengine.engine.options.EngineOptions)
	 */
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
		mEngine = new Engine(pEngineOptions);
		mEngine.enableVibrator(getApplicationContext());
		
		return mEngine;
	}
	
	
}
